package com.cvmars.jianyou.module.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.model.ImageUploadModel;
import com.cvmars.jianyou.model.UserModel;
import com.cvmars.jianyou.module.model.FindModel;
import com.cvmars.jianyou.ui.CircleImageView;
import com.cvmars.jianyou.utils.ImageUtils;
import com.cvmars.jianyou.utils.UtilHelper;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by hehaifeng on 2018/12/19.
 */

public class HomeFindAdapter extends BaseQuickAdapter<FindModel, BaseViewHolder> {


    Context mContext;

    public HomeFindAdapter(Context context, int layoutResId, @Nullable List<FindModel> data) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, FindModel item) {

        helper.setText(R.id.findContent, item.text);
        RecyclerView gridView = helper.getView(R.id.find_grid);
        gridView.setLayoutManager(new GridLayoutManager(mContext, 3));

        ImageView ivPic = helper.getView(R.id.iv_image);

        String create_at = item.create_at;
        Date time = UtilHelper.parseDate(create_at);

        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        int month = cal.get(Calendar.MONTH) + 1  ;
        int day = cal.get(Calendar.DAY_OF_MONTH);


        helper.setText(R.id.txt_time, day+"")
                .setText(R.id.txt_time_day, month+"月");
        ivPic.setVisibility(View.GONE);
        gridView.setVisibility(View.VISIBLE);
        List<ImageUploadModel> images = item.images;
        if (images != null) {

            if (images.size() > 1) {

                FindHomeAdapter imgAdapter = new FindHomeAdapter(mContext, R.layout.item_find_img, images);
//                gridView.addItemDecoration(new SpacesItemDecoration(DensityUtils.dip2px(mContext, 3)));
                gridView.setAdapter(imgAdapter);
            } else if(images.size() == 1) {
                ivPic.setVisibility(View.VISIBLE);
                gridView.setVisibility(View.GONE);
                ImageUtils.loadImage(mContext, images.get(0).url, ivPic);
            }
        }
    }
}
